package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public Result addSetmeal(SetmealDTO setmealDTO) {
        //先新增套餐
        Setmeal setmeal =Setmeal.builder()
                .categoryId(setmealDTO.getCategoryId())
                .name(setmealDTO.getName())
                .price(setmealDTO.getPrice())
                .status(setmealDTO.getStatus())
                .description(setmealDTO.getDescription())
                .image(setmealDTO.getImage())
                .createTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        int insert = setmealMapper.insert(setmeal);
        //如果新增套餐成功,新增套餐菜品
        if(insert>0){
            List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            for (int i = 0; i < setmealDishes.size(); i++) {
                setmealDishes.get(i).setSetmealId(setmeal.getId());

            }
            setmealDishMapper.insertBatch(setmealDishes);
            }else {
            //新增套餐失败 抛出异常
            throw new RuntimeException("新增套餐失败");
        }
        return Result.success(MessageConstant.ADD_SUCCESS);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);


        return Result.success(new PageResult(page.getTotal(), page.getResult()));
    }
    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result delete(List<Long> ids) {
        int delete = setmealMapper.delete(ids);
        if (delete>0){
            //删除套餐菜品
            for (int i = 0; i < ids.size(); i++) {
                setmealDishMapper.deleteById(ids.get(i));
            }

        }
        String message ="成功删除"+delete+"个套餐";
        return Result.success(message);
    }

    /**
     * 起售停售
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result startOrStop(Integer status, Long id) {
        int i = setmealMapper.startOrStop(status, id);
        return i>0?Result.success(MessageConstant.ADD_SUCCESS):Result.error(MessageConstant.UNKNOWN_ERROR);


    }


    /**
     * 修改套餐
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional
    public Result update(SetmealDTO dishDTO) {
        //修改套餐
        Setmeal setmeal =Setmeal.builder()
                .id(dishDTO.getId())
                .categoryId(dishDTO.getCategoryId())
                .name(dishDTO.getName())
                .price(dishDTO.getPrice())
                .status(dishDTO.getStatus())
                .description(dishDTO.getDescription())
                .image(dishDTO.getImage())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        int update = setmealMapper.update(setmeal);
        if (update>0){
            //先删除菜品数据
            setmealDishMapper.deleteById(dishDTO.getId());
            System.out.println("这里是dishDTO.getSetmealDishes()"+dishDTO.getSetmealDishes());
            //再添加菜品数据
            List<SetmealDish> setmealDishes = dishDTO.getSetmealDishes();
            for (SetmealDish originalFlavor : setmealDishes) {
                originalFlavor.setSetmealId(dishDTO.getId());
            }
            setmealDishMapper.insertBatch(dishDTO.getSetmealDishes());
        }else
            throw new RuntimeException("修改套餐失败");
        String msg = "修改套餐成功";
        return Result.success(msg);
    }

    @Override
    public Result<SetmealVO> getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
